/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package z.h.w.jar.kit.method;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author EnzoZhong
 */
public class Self {

      /**
       *
       * @return
       */
      public static Font[] getAllFont () {
            String[] fontList =
                     GraphicsEnvironment.getLocalGraphicsEnvironment ().getAvailableFontFamilyNames ();
            List<Font> fontNameList = new ArrayList<> ( 31 );
            for ( String name : fontList ) {
                  fontNameList.add ( new Font ( name , Font.PLAIN , 36 ) );
            }
            return To.list2Array ( fontNameList );

      }

      /**
       *
       * @return
       */
      public static String[] getAllFontName () {
            return GraphicsEnvironment.getLocalGraphicsEnvironment ().getAvailableFontFamilyNames ();
      }

      /**
       *
       * @return
       */
      public static Font[] getChineseFonts () {
            String[] fontList =
                     GraphicsEnvironment.getLocalGraphicsEnvironment ().getAvailableFontFamilyNames ();
            List<Font> fontNameList = new ArrayList<> ( 31 );
            for ( String name : fontList ) {
                  Pattern pattern = Pattern.compile ( "[a-zA-Z0-9\\[].*" );
                  Matcher matcher = pattern.matcher ( name );
                  Boolean b = matcher.matches ();
                  if (  ! b ) {
                        fontNameList.add ( new Font ( name , Font.PLAIN , 36 ) );
                  }
            }
            return To.list2Array ( fontNameList );
      }

      /**
       *
       * @return
       */
      public static String[] getChineseFontName () {
            String[] fontList =
                     GraphicsEnvironment.getLocalGraphicsEnvironment ().getAvailableFontFamilyNames ();
            List<String> fontNameList = new ArrayList<> ( 31 );
            for ( String name : fontList ) {
                  Pattern pattern = Pattern.compile ( "[a-zA-Z0-9\\[].*" );
                  Matcher matcher = pattern.matcher ( name );
                  Boolean b = matcher.matches ();
                  if (  ! b ) {
                        fontNameList.add ( name );
                  }
            }
            return To.list2Array ( fontNameList );
      }

      public static Integer firstIndex ( Object[] array , Object target ) {
            for ( int x = 0 ; x < array.length ; x ++ ) {
                  if ( array[x] == target ) {
                        return x;
                  }
            }
            return -1;
      }

      @SuppressWarnings ( "unchecked" )
      public synchronized static <T> T[] subArray ( T[] array , Integer b , Integer e ) {
            int len = e - b;
            if ( len == 0 ) {
                  return ( T[] ) new Object[ 0 ];
            } else {
                  T[] tmp = Maker.makeArray ( array[0].getClass () , len );
                  for ( int x = b ; x < e ; x ++ ) {
                        tmp[x - b] = array[x];
                  }
                  return tmp;
            }
      }

      @SuppressWarnings ( "unchecked" )
      public synchronized static <T> T[] subArray ( Class<T> clazz , T[] array , Integer b , Integer e ) {
            int len = e - b;
            if ( len == 0 ) {
                  return ( T[] ) new Object[ 0 ];
            } else {
                  T[] tmp = Maker.makeArray ( clazz , len );
                  for ( int x = b ; x < e ; x ++ ) {
                        tmp[x - b] = array[x];
                  }
                  return tmp;
            }
      }

      /**
       *
       * @param array
       * @param target
       *
       * @return
       */
      public static Integer lastIndex ( Object[] array , Object target ) {
            for ( int x = array.length - 1 ; x >= 0 ; x -- ) {
                  if ( array[x] == target ) {
                        return x;
                  }
            }
            return -1;
      }

      /**
       *
       * @return
       */
      public static Map<String , Integer> getFontType () {
            Map<String , Integer> re = new HashMap<> ( 5 );
            re.put ( "普通" , Font.PLAIN );
            re.put ( "斜体" , Font.ITALIC );
            re.put ( "粗体" , Font.BOLD );
            return re;
      }

      public synchronized static Boolean isNull ( Object[] array ) {
            Boolean booleanA1 = false;
            if ( array == null ) {
                  booleanA1 = true;
            } else if ( array.length == 0 ) {
                  booleanA1 = true;
            }
            return booleanA1;
      }

      /**
       *
       * @param cla
       *
       * @return
       */
      public static Stack<Field> getFieldStack ( Class<?> cla ) {
            Stack<Class<?>> classStack = getInheritStack ( cla );
            Stack<Field> fieldList = new Stack<> ();
            while (  ! classStack.isEmpty () ) {
                  Class<?> clazz = classStack.pop ();
                  Field[] fields = clazz.getDeclaredFields ();
                  fieldList.addAll ( Arrays.asList ( fields ) );
            }
            return fieldList;
      }

      /**
       *
       * @param obj
       *
       * @return
       */
      public static Stack<Field> getFieldStack ( Object obj ) {
            return getFieldStack ( obj.getClass () );
      }

      /**
       *
       * @param obj
       *
       * @return
       */
      public static Stack<Class<?>> getInheritStack ( Object obj ) {
            return getInheritStack ( obj.getClass () );
      }

      public static Stack<Class<?>> getInheritStack ( Class<?> clazz ) {
            Stack<Class<?>> classStack = new Stack<> ();
            while ( true ) {
                  classStack.add ( clazz );
                  clazz = clazz.getSuperclass ();
                  if ( clazz.equals ( Object.class ) ) {
                        break;
                  }
            }
            return classStack;
      }

      public static List<String> getMAC () {
            ArrayList<String> arrayList = new ArrayList<> ( 5 );
            Enumeration<NetworkInterface> enu = null;
            try {
                  enu = NetworkInterface.getNetworkInterfaces ();
            } catch ( SocketException ex ) {
                  Logger.getLogger ( ED.class.getName () ).log ( Level.SEVERE , null , ex );
            }
            while ( enu.hasMoreElements () ) {
                  NetworkInterface networkInterface = enu.nextElement ();

                  String mac = ED.getMACAddress ( networkInterface );
                  if ( mac.length () == 17 ) {
                        arrayList.add ( mac );
                  }

            }
            return arrayList;
      }

      public static Integer getCPU () {
            return Runtime.getRuntime ().availableProcessors ();
      }

      public static Annotation findAnnotation ( Annotation[] annos , String target ) {
            for ( Annotation anno : annos ) {
                  if ( anno.toString ().contains ( "JsonAdaptor" ) ) {
                        return anno;
                  }
            }
            return null;
      }

}
